package cn.bellychang.leetcode.question139;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2021/3/15
 */
public class Solution2 {

    public boolean wordBreak(String s, List<String> wordDict) {

        LinkedList<Integer> queue = new LinkedList<>();
        queue.add(0);

        HashSet<Integer> set = new HashSet<>();
        set.add(0);

        while (!queue.isEmpty()) {
            Integer index = queue.remove();
            for (int i = index; i <= s.length(); i++) {

                String temp = s.substring(index, i);
                if (wordDict.contains(temp)) {
                    if (i == s.length()) {
                        return true;
                    }

                    // 当set不包含 两个都增加
                    if (!set.contains(i)) {
                        queue.add(i);
                        set.add(i);
                    }
                }
            }
        }
        return false;
    }
}
